package com.donggua.aichebao.login;

import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;

import com.xx.xiawan.net.DGJsonRequest;
import com.xx.xiawan.net.DGJsonResult;
import com.xx.xiawan.net.DGRequestListener;
import com.xx.xiawan.net.GetRequest;
import com.dg.acb.utils.configdata.XXConstants;
import com.dg.acb.utils.configdata.XXUserInfoData;
import com.donggua.aichebao.commonui.load.LoadInputLayout;
import com.donggua.aichebao.commonui.setting.DGSettingBase;

import org.json.JSONException;
import org.json.JSONObject;

/**
 * Created by fzh on 2017/8/22.
 */

public class DGLoginRegist extends DGSettingBase implements View.OnClickListener,DGRequestListener{
    private Button btnNext;
    private Button btnGetCheckCode;
    /*
    private TextAndEditLayout taeCheckCode;
    private TextAndEditLayout taePassword;
    private TextAndEditLayout taeSurePasswor;
    private TextAndEditLayout taeAccount;
    */
    private LoadInputLayout LiRegist_phone_check;
    private LoadInputLayout LiRegist_check_code;
    private LoadInputLayout LiRegist_password;
    private JSONObject mJson;
    public DGLoginRegist(Activity act, View view) {
        super(act, view);
        this.setTitleName(R.string.loginRegistTitle);
        /*
        taeAccount = (TextAndEditLayout)mMainView.findViewById(R.id.tae_Account);
        taeCheckCode = (TextAndEditLayout)mMainView.findViewById(R.id.tae_CheckCode);
        taePassword = (TextAndEditLayout)mMainView.findViewById(R.id.tae_Password);
       taeSurePasswor = (TextAndEditLayout)mMainView.findViewById(R.id.tae_SurePasswor);
       */
        LiRegist_phone_check = (LoadInputLayout)mMainView.findViewById(R.id.regist_phone_check);
        LiRegist_check_code = (LoadInputLayout)mMainView.findViewById(R.id.regist_check_code);
        LiRegist_password = (LoadInputLayout)mMainView.findViewById(R.id.regist_password);
        btnNext = (Button)mMainView.findViewById(R.id.btnOk);
        btnGetCheckCode = (Button)mMainView.findViewById(R.id.btnGetCheckCode);
        btnNext.setOnClickListener(this);
        btnGetCheckCode.setOnClickListener(this);

    }

    @Override
    public void onClick(View view) {
        int id = view.getId();
        if(id == R.id.btnOk){
            DGJsonRequest jsonRequest = new DGJsonRequest(mActivity, XXConstants.URI_REGISTER,mJson,DGLoginRegist.this);
            jsonRequest.startNoLock();
        }else if(R.id.btnGetCheckCode == id){
            getPhoneCode();
        }
    }

    private void getPhoneCode(){
        String url = XXConstants.URI_GET_SMS_CODE +"?"+"phone=" + "15060136817";
        DGRequestListener listener = new DGRequestListener() {
            @Override
            public void onSuccess(DGJsonResult result) {
                Log.e("regist"," result is "+result.result);
                mJson = new JSONObject();
                try {
                    mJson.put(XXConstants.USER_KEY,"15060136817");
                    String password = LiRegist_password.getInputText();
                    Log.e("regist"," password is "+password);
                    mJson.put("password",password);
                    int code = Integer.parseInt(result.result);
                    Log.e("regist"," code is "+code);
                    mJson.put(XXConstants.VERIFICATION_KEY,code);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onError(String error) {
                Log.e("regist"," error is "+error);
            }
        };
        GetRequest getRequest = new GetRequest(url,mActivity,listener);
        getRequest.startNoLock();
    }
    @Override
    public void onSuccess(DGJsonResult result) {
        Log.e("regist"," result is "+result.result);
        if(result.isSuccess()){
            JSONObject jsonObject = null;
            try {
                jsonObject = new JSONObject(result.result);
                String token = jsonObject.getString(XXConstants.TOKEN_KEY);
                long id = jsonObject.getLong(XXConstants.ID_KEY);
                String user = jsonObject.getString(XXConstants.USER_KEY);
                int status = jsonObject.getInt(XXConstants.STATUS_KEY);
                //XXUserInfoData userData = new XXUserInfoData(id,status,user,token);
                XXUserInfoData userData = new XXUserInfoData();
                Intent intent = new Intent("com.dg.acb.merchant.authenticate");
                intent.putExtra(XXConstants.USER_DATA_KEY,userData);
                intent.setPackage(mActivity.getPackageName());
                mActivity.startActivity(intent);
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }
    }

    @Override
    public void onError(String error) {

    }
}
